package lium.lbs.lbsim.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.*;
import cn.jpush.im.android.api.JMessageClient;
import com.buz.hjcdriver.bean.AResultBean;
import com.kyleduo.switchbutton.SwitchButton;
import com.lmlibrary.Constants;
import com.lmlibrary.bean.ResponseBean;
import com.lzy.okgo.model.Response;
import lium.lbs.lbsim.R;
import lium.lbs.lbsim.bean.SetGroupBean;
import lium.lbs.lbsim.jgchat.application.GJApp;
import lium.lbs.lbsim.utils.SpUtils;
import lium.lbs.lbsim.utils.ToastUtils;
import lium.lbs.lbsim.utils.callbck.JsonCallback;

import java.util.HashMap;

public class SetGroupActivity extends BaseActivity implements View.OnClickListener {

    private ImageView left_img;
    private LinearLayout left_bar;
    private TextView center_text;
    private LinearLayout center_bar;
    private TextView right_text;
    private ImageView right_img;
    private LinearLayout right_bar;
    private SwitchButton swb_needfee;
    private RadioButton rb_nobody;
    private RadioButton rb_needyanzheng;
    private RadioButton rb_shenmfen;
    private RadioButton rb_needanswer;
    private TextView et_wenti;
    private TextView et_answer;
    private LinearLayout ll_answer;
    private RadioGroup rg_renzheng;
    private RadioButton rb_one;
    private RadioButton rb_five;
    private RadioButton rb_ten;
    private RadioButton rb_other;
    private RadioGroup rg_moeny;
    private RadioGroup rg_way;
    private SwitchButton swb_gotoaddfriend;
    private SwitchButton swb_gotoseeinfor;
    private SwitchButton swb_stopall;
    private TextView tv_stopone;
    private TextView tv_zhuanrang;
    private TextView tv_jiesan;
    private String groupId;
    private MyDialog mMyDialog;
    private String jggroupId;
    private LinearLayout yc_ll;
    private String stringAddWay = "1";//入群方式
    private String stringIdAuth = "1";//验证方式
    private String stringIsPay = "1";//是否需要支付费用入群
    private String stringisview = "1";//是否需可以看信息
    private String stringallBan = "1";//是否需全体禁言
    private String stringfindfriend = "1";//是否需全体禁言
    private LinearLayout ll_own;
    private TextView tv_setmageman;
    private TextView tv_kick;
    private String nameGourp;
    private String memberRole;


    HashMap<String, String> parrmt = new HashMap<String, String>();


    @Override
    protected int getLayoutId() {
        return R.layout.activity_set_group;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setTitleWithBack("群设置");
        groupId = getIntent().getStringExtra(GJApp.MY_GROUP_ID);
        jggroupId = getIntent().getStringExtra(GJApp.GROUP_ID);

        memberRole = getIntent().getStringExtra("memberRole");
        yc_ll = (LinearLayout) findViewById(R.id.yc_ll);
        if (TextUtils.isEmpty(jggroupId)) {
            yc_ll.setVisibility(View.GONE);
        } else {
            yc_ll.setVisibility(View.VISIBLE);
        }
        if (TextUtils.isEmpty(groupId)) {
            groupId = SpUtils.getInstance().get(Constants.groupIDLS, "").toString();
        }
        left_img = (ImageView) findViewById(R.id.left_img);
        left_img.setOnClickListener(this);

        left_img = (ImageView) findViewById(R.id.left_img);
        left_img.setOnClickListener(this);

        ll_own = (LinearLayout) findViewById(R.id.ll_own);
        ll_own.setOnClickListener(this);

        tv_setmageman = (TextView) findViewById(R.id.tv_setmageman);
        tv_setmageman.setOnClickListener(this);

        tv_kick = (TextView) findViewById(R.id.tv_kick);
        tv_kick.setOnClickListener(this);

        left_bar = (LinearLayout) findViewById(R.id.left_bar);
        left_bar.setOnClickListener(this);
        center_text = (TextView) findViewById(R.id.center_text);
        center_text.setOnClickListener(this);
        center_bar = (LinearLayout) findViewById(R.id.center_bar);
        center_bar.setOnClickListener(this);
        right_text = (TextView) findViewById(R.id.right_text);
        right_text.setOnClickListener(this);
        right_img = (ImageView) findViewById(R.id.right_img);
        right_img.setOnClickListener(this);
        right_bar = (LinearLayout) findViewById(R.id.right_bar);
        right_bar.setOnClickListener(this);
        swb_needfee = (SwitchButton) findViewById(R.id.swb_needfee);
        swb_needfee.setOnClickListener(this);

        rb_nobody = (RadioButton) findViewById(R.id.rb_nobody);
        rb_nobody.setOnClickListener(this);
        rb_needyanzheng = (RadioButton) findViewById(R.id.rb_needyanzheng);
        rb_needyanzheng.setOnClickListener(this);
        rb_shenmfen = (RadioButton) findViewById(R.id.rb_shenmfen);
        rb_shenmfen.setOnClickListener(this);
        rb_needanswer = (RadioButton) findViewById(R.id.rb_needanswer);
        rb_needanswer.setOnClickListener(this);
        et_wenti = (TextView) findViewById(R.id.et_wenti);
        et_wenti.setOnClickListener(this);
        et_answer = (TextView) findViewById(R.id.et_answer);
        et_answer.setOnClickListener(this);
        ll_answer = (LinearLayout) findViewById(R.id.ll_answer);
        ll_answer.setOnClickListener(this);
        rg_renzheng = (RadioGroup) findViewById(R.id.rg_renzheng);
        rg_renzheng.setOnClickListener(this);
        rb_one = (RadioButton) findViewById(R.id.rb_one);
        rb_one.setOnClickListener(this);
        rb_five = (RadioButton) findViewById(R.id.rb_five);
        rb_five.setOnClickListener(this);
        rb_ten = (RadioButton) findViewById(R.id.rb_ten);
        rb_ten.setOnClickListener(this);
        rb_other = (RadioButton) findViewById(R.id.rb_other);
        rb_other.setOnClickListener(this);
        rg_moeny = (RadioGroup) findViewById(R.id.rg_moeny);
        rg_moeny.setOnClickListener(this);
        rg_way = (RadioGroup) findViewById(R.id.rg_way);
        rg_way.setOnClickListener(this);
        swb_gotoaddfriend = (SwitchButton) findViewById(R.id.swb_gotoaddfriend);
        swb_gotoaddfriend.setOnClickListener(this);
        swb_gotoseeinfor = (SwitchButton) findViewById(R.id.swb_gotoseeinfor);
        swb_gotoseeinfor.setOnClickListener(this);
        swb_stopall = (SwitchButton) findViewById(R.id.swb_stopall);
        swb_stopall.setOnClickListener(this);
        tv_stopone = (TextView) findViewById(R.id.tv_stopone);
        tv_stopone.setOnClickListener(this);
        tv_zhuanrang = (TextView) findViewById(R.id.tv_zhuanrang);
        tv_zhuanrang.setOnClickListener(this);
        tv_jiesan = (TextView) findViewById(R.id.tv_jiesan);
        tv_jiesan.setOnClickListener(this);
        if (!TextUtils.isEmpty(memberRole)){
            if ("1".equals(memberRole)){
                ll_own.setVisibility(View.VISIBLE);
                yc_ll.setVisibility(View.VISIBLE);
            } else if ("2".equals(memberRole)){
                ll_own.setVisibility(View.GONE);
                yc_ll.setVisibility(View.VISIBLE);
            } else {
                ll_own.setVisibility(View.GONE);
                yc_ll.setVisibility(View.GONE);
            }

        }

        //入群方式
        rg_way.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {

                    case R.id.rb_nobody:
                        rb_nobody.setChecked(true);
                        rg_renzheng.setVisibility(View.GONE);
                        stringAddWay = "2";
                        break;
                    case R.id.rb_needyanzheng:
                        rb_needyanzheng.setChecked(true);
                        rg_renzheng.setVisibility(View.VISIBLE);
                        stringAddWay = "3";
                        break;
                }
                parrmt.clear();
                parrmt.put("addWay", stringAddWay);
                setSysteMessages(parrmt);
            }
        });
        //身份认证
        rg_renzheng.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_shenmfen:
                        rb_shenmfen.setChecked(true);
                        ll_answer.setVisibility(View.GONE);
                        stringIdAuth = "1";
                        break;
                    case R.id.rb_needanswer:
                        rb_needanswer.setChecked(true);
                        ll_answer.setVisibility(View.VISIBLE);
                        stringIdAuth = "2";
                        break;
                }
                parrmt.clear();
                parrmt.put("idAuth", stringIdAuth);
                setSysteMessages(parrmt);
            }
        });

        ll_answer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                putquestinAnswer();
            }
        });
        swb_needfee.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    stringIsPay = "1";
                    rg_moeny.setVisibility(View.VISIBLE);
                } else {
                    stringIsPay = "0";
                    rg_moeny.setVisibility(View.GONE);
                    parrmt.clear();
                    parrmt.put("ispay", stringIsPay);
                    setSysteMessages(parrmt);
                }

            }
        });
        rg_moeny.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                parrmt.put("ispay", stringIsPay);
                switch (checkedId) {
                    case R.id.rb_one:
                        rb_one.setChecked(true);
                        break;
                    case R.id.rb_five:
                        rb_five.setChecked(true);
                        break;
                    case R.id.rb_ten:
                        rb_ten.setChecked(true);
                        break;
                    case R.id.rb_other:
                        rb_other.setChecked(true);
                        break;
                }
            }
        });
        //允许群内查看信息
        swb_gotoseeinfor.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    stringisview = "1";
                } else {
                    stringisview = "0";
                }
                parrmt.clear();
                parrmt.put("isview", stringisview);
                setSysteMessages(parrmt);
            }
        });
        //全体禁言
        swb_stopall.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    stringallBan = "1";
                } else {
                    stringallBan = "0";

                }
                parrmt.clear();
                parrmt.put("allBan", stringallBan);
                setSysteMessages(parrmt);
            }
        });
        //允许群内添加好友
        swb_gotoaddfriend.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    stringfindfriend = "1";
                } else {
                    stringfindfriend = "0";

                }
                parrmt.clear();
                parrmt.put("isaddeach", stringfindfriend);
                setSysteMessages(parrmt);
            }
        });

    }

    private void showInputDialog() {
        View view = getLayoutInflater().inflate(R.layout.dialog_toput_moeny, null);
        mMyDialog = new MyDialog(this, 0, 0, view, R.style.dialog_default_style);
        mMyDialog.setCancelable(true);
        mMyDialog.show();
        TextView moeny = view.findViewById(R.id.tv_moeny);
        view.findViewById(R.id.tv_tips_dialog_confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(moeny.getText())) {
                    ToastUtils.showLongToast("请输入入群收取的金额");
                    return;
                }
                parrmt.put("ispay", stringIsPay);
                parrmt.put("price", moeny.getText().toString());
                setSysteMessages(parrmt);
                mMyDialog.cancel();
            }
        });
        view.findViewById(R.id.tv_tips_dialog_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMyDialog.cancel();
            }
        });
    }

    private void putquestinAnswer() {
        View view = getLayoutInflater().inflate(R.layout.dialog_toput_question, null);
        mMyDialog = new MyDialog(this, 0, 0, view, R.style.dialog_default_style);
        mMyDialog.setCancelable(true);
        mMyDialog.show();
        TextView tv_question = view.findViewById(R.id.tv_question);
        TextView tv_answer = view.findViewById(R.id.tv_answer);
        view.findViewById(R.id.tv_tips_dialog_confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(tv_question.getText().toString())) {
                    ToastUtils.showLongToast("请输入入群问题");
                    return;
                }
                if (TextUtils.isEmpty(tv_answer.getText())) {
                    ToastUtils.showLongToast("请输入入群答案");
                    return;
                }

                parrmt.clear();
                parrmt.put("question", tv_question.getText().toString());
                parrmt.put("answer", tv_answer.getText().toString());
                setSysteMessages(parrmt);

                mMyDialog.cancel();
            }
        });
        view.findViewById(R.id.tv_tips_dialog_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMyDialog.cancel();
            }
        });

    }

    public class MyDialog extends Dialog {
        public MyDialog(Context context, int width, int height, View layout, int style) {
            super(context, style);
            setContentView(layout);
            Window window = getWindow();
            WindowManager.LayoutParams params = window.getAttributes();
            params.gravity = Gravity.CENTER;
            window.setAttributes(params);
        }
    }

    @Override
    protected void getNetWorkData() {
        getSysteMessages();
    }

    public void getSysteMessages() {
        HashMap<String, String> parrmt = new HashMap<String, String>();
        parrmt.put("memberId", (String) SpUtils.getInstance().get(Constants.memberId, ""));
        parrmt.put("groupId", groupId);
        postData("/group/groupManage", parrmt, new JsonCallback<ResponseBean<SetGroupBean>>(this) {
            @Override
            public void onSuccess(Response<ResponseBean<SetGroupBean>> response) {
                setData(response.body().data);

            }
        });

    }


    public void setSysteMessages(HashMap<String, String> parrmt) {
        parrmt.put("memberId", (String) SpUtils.getInstance().get(Constants.memberId, ""));
        parrmt.put("groupId", groupId);
        postData("/group/updateGroupInfo", parrmt, new JsonCallback<ResponseBean<AResultBean>>(this) {
            @Override
            public void onSuccess(Response<ResponseBean<AResultBean>> response) {
                setDataReule(response.body().msg);
            }
        });

    }

    private void setDataReule(String msg) {
        ToastUtils.showToast(msg == null ? "设置成功" : msg);
        getSysteMessages();
    }

    public void setData(SetGroupBean setGroupBean) {

        if (setGroupBean.getGroup().getAllBan())
        if (setGroupBean.getGroup().getIsaddeach()) {
            swb_gotoaddfriend.setChecked(true);
        } else {
            swb_gotoaddfriend.setChecked(false);
        }
        tv_stopone.setText(setGroupBean.getGroup().getOne_ban() == null ? "" : setGroupBean.getGroup().getOne_ban());

        if (setGroupBean.getGroup().getIspay()) {
            swb_needfee.setChecked(true);
            rg_moeny.setVisibility(View.VISIBLE);
            switch (setGroupBean.getGroup().getPrice() == null ? "0" : setGroupBean.getGroup().getPrice()) {
                case "1":
                    rb_one.setChecked(true);
                case "1.0":
                    rb_one.setChecked(true);
                case "1.00":
                    rb_one.setChecked(true);
                    break;
                case "5":
                    rb_five.setChecked(true);
                case "5.0":
                    rb_five.setChecked(true);
                case "5.00":
                    rb_five.setChecked(true);
                    break;
                case "10":
                    rb_ten.setChecked(true);
                    break;
                case "10.0":
                    rb_ten.setChecked(true);
                    break;
                case "10.00":
                    rb_ten.setChecked(true);
                    break;
                default:
                    rb_other.setChecked(true);
                    rb_other.setText(setGroupBean.getGroup().getPrice() + "元");
                    break;

            }
        } else {
            swb_needfee.setChecked(false);
            rg_moeny.setVisibility(View.GONE);
        }
        if (setGroupBean.getGroup().getAllBan()) {
            swb_stopall.setChecked(true);
        } else {
            swb_stopall.setChecked(false);
        }
        if (setGroupBean.getGroup().getIsview()) {
            swb_gotoseeinfor.setChecked(true);
        } else {
            swb_gotoseeinfor.setChecked(false);
        }

        switch (setGroupBean.getGroup().getAddWay()) {

            case "2":
                rb_nobody.setChecked(true);
                rg_renzheng.setVisibility(View.GONE);
                break;
            case "3":
                rb_needyanzheng.setChecked(true);
                rg_renzheng.setVisibility(View.VISIBLE);
                switch (setGroupBean.getGroup().getIdAuth()) {
                    case "1":
                        rb_shenmfen.setChecked(true);
                        ll_answer.setVisibility(View.GONE);
                        break;
                    case "2":
                        rb_needanswer.setChecked(true);
                        ll_answer.setVisibility(View.VISIBLE);
                        et_wenti.setText(setGroupBean.getGroup().getQuestion() == null ? "" : setGroupBean.getGroup().getQuestion());
                        et_answer.setText(setGroupBean.getGroup().getAnswer() == null ? "" : setGroupBean.getGroup().getAnswer());
                        break;
                }
                break;
        }
        if (setGroupBean.getGroup().getIsview()) {
            swb_gotoseeinfor.setChecked(true);
        } else {
            swb_gotoseeinfor.setChecked(false);
        }

    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.et_wenti:
                putquestinAnswer();
                break;
            case R.id.et_answer:
                putquestinAnswer();
                break;
            case R.id.left_bar:
            case R.id.left_img:
                finish();
                break;
            case R.id.tv_stopone:
                intent.setClass(this, ForbiddenWordsActivity.class);
                intent.putExtra(GJApp.MY_GROUP_ID, groupId);
                intent.putExtra(GJApp.GROUP_ID, jggroupId);
                intent.putExtra("type", 0);
                intent.putExtra("title", "成员禁言");
                startActivity(intent);
                break;
            case R.id.tv_zhuanrang:
                intent.setClass(this, KickPeopleActivity.class);
                intent.putExtra(GJApp.MY_GROUP_ID, groupId);
                intent.putExtra(GJApp.GROUP_ID, jggroupId);
                intent.putExtra("type", 0);
                intent.putExtra("title", "群主转让");
                startActivity(intent);
                break;
            case R.id.tv_kick:
                intent.setClass(this, KickPeopleActivity.class);
                intent.putExtra(GJApp.MY_GROUP_ID, groupId);
                intent.putExtra(GJApp.GROUP_ID, jggroupId);
                intent.putExtra("type", 1);
                intent.putExtra("title", "踢出群成员");
                startActivity(intent);
                break;
            case R.id.tv_setmageman:
                intent.setClass(this, SetGroupMagemenActivity.class);
                intent.putExtra(GJApp.MY_GROUP_ID, groupId);
                intent.putExtra(GJApp.GROUP_ID, jggroupId);
                intent.putExtra("type", 0);
                intent.putExtra("title", "取消管理员设置");
                startActivity(intent);
                break;
            case R.id.tv_jiesan:
                JMessageClient.adminDissolveGroup(Long.parseLong(jggroupId), null);
                ToastUtils.showToast("已设置解散群");
                finish();
                break;

            case R.id.rb_other:
                showInputDialog();
                break;
            case R.id.rb_one:
                parrmt.put("price", "1");
                parrmt.put("ispay", stringIsPay);
                setSysteMessages(parrmt);
                break;
            case R.id.rb_five:
                parrmt.put("price", "5");
                parrmt.put("ispay", stringIsPay);
                setSysteMessages(parrmt);
                break;
            case R.id.rb_ten:
                parrmt.put("price", "10");
                parrmt.put("ispay", stringIsPay);
                setSysteMessages(parrmt);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        finishActivity(100);
    }
}
